package com.zzyl.serve.service.impl;

import java.util.List;

import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.Dto.NursingPlanDto;
import com.zzyl.common.utils.DateUtils;

import com.zzyl.serve.Dto.NursingProjectPlanDto;
import com.zzyl.serve.Vo.NursingPlanVo;
import com.zzyl.serve.Vo.NursingProjectPlanVo;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class NursingPlanServiceImpl             implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlan selectNursingPlanById(Integer id)
    {
        return nursingPlanMapper.selectNursingPlanById(id);
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }



    /**
     * 新增护理计划
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto dto)
    {
        //保存护理计划
        //属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        //批量保存护理项目计划关系
        Integer planId = nursingPlan.getId();
        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
        projectPlans.stream().forEach(projectPlan -> {
            projectPlan.setPlanId(planId);
        });
        return nursingProjectPlanMapper.batchInsert(projectPlans);

    }


    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
//        直接修改护理计划表
NursingPlan nursingPlan =new NursingPlan();
BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
nursingPlan.setId(nursingPlanDto.getId());
      int  count =        nursingPlanMapper.updateNursingPlan(nursingPlan);
//        删除关联表--新增
        if (nursingPlanDto.getProjectPlans() != null&& nursingPlanDto.getProjectPlans().size()>0)
        {
            nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(nursingPlan.getId());
//           新增
            nursingPlanDto.getProjectPlans().stream().forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
            nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans());
        }
        return count;
    }

    /**
     * 批量删除护理计划
     * 
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long id)
    {
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(id.intValue());

        return nursingPlanMapper.deleteNursingPlanById(id.intValue());
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        return nursingPlanMapper.deleteNursingPlanById(id);
    }

//根据id查询护理计划及项目计划
    @Override
    public NursingPlanVo selectNusringPlanAndProject(Integer id) {
//        查询护理计划

        NursingPlan plan = this.nursingPlanMapper.selectNursingPlanById(id);
//        查询关联表
        List<NursingProjectPlanVo> projectPlans   =    nursingProjectPlanMapper.selectNursingProjectPlanByPlanId(id);
//        组合数据
        NursingPlanVo nursingPlanVo = new NursingPlanVo();

        BeanUtils.copyProperties(plan,nursingPlanVo);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }
}
